package com.ordos.recruit.module.audit.controller;

import com.ordos.recruit.aop.anno.UnInternetApi;
import com.ordos.recruit.module.audit.entity.RecruitmentApplicationAuditDto;
import com.ordos.recruit.module.audit.entity.RecruitmentApplicationAuditPo;
import com.ordos.recruit.module.audit.service.ApplyAuditService;
import com.wonders.commons.util.exception.ApiException;
import com.wonders.commons.util.model.ValidatorResult;
import com.wonders.commons.util.response.ApiResponse;
import com.wonders.commons.validation.ObjectValidator;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 审核Controller
 *
 * @author jin
 */
@RequestMapping("audit")
@RestController
public class ApplyAuditController {

    private final ApplyAuditService applyAuditService;
    private final ObjectValidator objectValidator;

    public ApplyAuditController(ApplyAuditService applyAuditService, ObjectValidator objectValidator) {
        this.applyAuditService = applyAuditService;
        this.objectValidator = objectValidator;
    }

    /**
     * 保存审核记录.
     * @param auditDto auditDto
     * @return ApiResponse
     */
    @PostMapping("saveOrUpdate")
    @UnInternetApi
    public ApiResponse<String> saveOrUpdateAudit(@RequestBody RecruitmentApplicationAuditDto auditDto) {
        ValidatorResult<RecruitmentApplicationAuditDto> validatorResult = objectValidator.objectValidator(auditDto);
        if(validatorResult.isHasErrors()) {
            throw new ApiException(ApiResponse.dataError(0, validatorResult.getError()));
        }
        return applyAuditService.saveOrUpdateAudit(auditDto);
    }

    /**
     * 查看审核记录。
     *
     * @param audit audit
     * @return ApiResponse
     */
    @PostMapping("allView")
    @UnInternetApi
    public ApiResponse<RecruitmentApplicationAuditPo> allView(@RequestBody RecruitmentApplicationAuditDto audit) {
        return ApiResponse.success(applyAuditService.allView(audit.getId()));
    }
}
